package cn.wolfcode.ws.mq;



import cn.wolfcode.ws.websocket.WebSocketServer;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@RocketMQMessageListener(
        consumerGroup = MQConstans.CREATE_ORDER_NOTIFY_GROUP,
        topic = MQConstans.SECKILL_ORDER_TOPIC,
        selectorExpression = MQConstans.CREATE_ORDER_SUCCESS_TAG + "||" + MQConstans.CREATE_ORDER_FAIL_TAG

)
@Component
public class OrderFailNotifyMQListener implements RocketMQListener<Map> {

    @SneakyThrows
    @Override
    public void onMessage(Map map) {
        try {
            System.out.println("进入到通知队列");
            Object uuid = map.get("uuid");
            Thread.sleep(3000);
            WebSocketServer webSocketServer = WebSocketServer.clients.get(uuid);
            if(webSocketServer != null) {
                webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(map));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
